local BagLogic = require("bag.BagLogic")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local ShopHelp = require("shop.ShopHelp")
local RoleFFI = require("role.RoleFFI")
local Msg = require("core.Msg")
local Log = require("common.Log")

function sendStatus(human, status)
  local sendMsg = Msg.gc.GC_MARKET_STATUS
  sendMsg.status = status
  Msg.send(sendMsg, human.fd)
end

function CG_MARKET_SELL(human, msg)
  local grid = human.db.bag[msg.bagPos]
  if grid.bind ~= 0 then
    return
  end
  if grid.lifeTime and grid.lifeTime ~= 0 then
    return
  end
  if grid.id == 0 or grid.cnt < msg.cnt then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MARKET_ERR_ITEM_NOT_ENOUGHT)
  end
  
  if not grid.uuid then
    assert()
  end
  if msg.yuanbao < 2 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.AUCTION_SALE_PRICE_TOO_LOW)
  end
  local tmp = {}
  tmp.id = grid.id
  tmp.cnt = msg.cnt
  tmp.uuid = RoleFFI.ffi_string(grid.uuid)
  tmp.yuanbao = msg.yuanbao
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_MARKET_SELL, tmp)  
  human.db.market_bagPos = msg.bagPos
  human.db.market_bagCnt = msg.cnt
--  Log.write(Log.LOGID_OSS_MARKET, human._id, human.account, human.name, human.db.lv, tmp.id, tmp.cnt, tmp.yuanbao, 1, tmp.uuid, 0)
end

function sell(human)--, oType, info)
  BagLogic.delItemByIndex(human, human.db.market_bagPos, human.db.market_bagCnt, "market")
end

function recycle(human, oType, info)
  if not BagLogic.checkCanAddItem(human, info.id, 0, info.cnt) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MARKET_BACK_BAG_GRID_NOT_ENOUGH)    
  end
  BagLogic.addItem(human, info.id, 0, info.cnt, "market_reback")
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_MARKET_BACK, info)
  Log.write(Log.LOGID_OSS_MARKET, human._id, human.account, human.name, human.db.lv, info.id, info.cnt, info.yuanbao, 3, info.uuid, 0)
end
--购买道具
function buy(human, oType, info)
  if ObjHuman.getYuanbao(human) < info.yuanbao then
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_MARKET_BUY, info)
    return ShopHelp.onNoRMB(human)
  end
  if not BagLogic.checkCanAddItem(human, info.itemId, 0, info.itemCnt) then
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_MARKET_BUY, info)
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.MARKET_BUY_BAG_GRID_NOT_ENOUGH)    
  end
  ObjHuman.decYuanbao(human, info.yuanbao, "market")
  BagLogic.addItem(human, info.itemId, 0, info.itemCnt, "market")
  sendStatus(human, 4)
  Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.MARKET_ERR_PICK_ITEM_SUCCESS)
  info.success = 1
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_MARKET_BUY, info)
  local shouxufei = 0
  if info.yuanbao < 10 then
    shouxufei = 1       
  else
    shouxufei = math.ceil(info.yuanbao * 0.1)
  end
  Log.write(Log.LOGID_OSS_MARKET, human._id, human.account, human.name, human.db.lv, info.itemId, info.itemCnt, info.yuanbao, 2, info._id, shouxufei)
end
    
InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_MARKET_BUY, buy)
InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_MARKET_BACK, recycle)
InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_MARKET_SELL, sell)